﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using vngoodlife.Singleton;
using vngoodlife.Util;
using System.IO;

namespace vngoodlife.Controllers
{
    public class ArticleSubjectController : Controller
    {

        #region BackEnd

        /// <summary>
        /// use in page ArticleEdit to get a subject quickly autocomplete, ajax
        /// </summary>
        /// <param name="term"></param>
        /// <returns></returns>
        [ Authorize(Roles = ConstantClass.articleAuthentication)]
        public ActionResult GetSubject(string term)
        {
            term = term.Trim();
            var artSubName = DataSingleton.Instance.GetLinQContext().ArticleSubjects
                             .Where(u => u.ArtSubjectName.IndexOf(term) != -1)
                             .OrderByDescending(u=>u.ArtSubjectID)
                             .Take(10)
                             .Select(u => new { label = u.ArtSubjectName, id = u.ArtSubjectID });
            if (artSubName.Count() == 0)
            {
                IEnumerable<ArticleSubject> subs = new ArticleSubject[] 
                                { 
                                  new ArticleSubject { ArtSubjectID = 0, ArtSubjectName = "Chủ đề này chưa có" } 
                                };
                var aa = subs.Select(u => new { label = u.ArtSubjectName, id = u.ArtSubjectID });
                return Json(aa.ToArray(), JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(artSubName.ToArray(), JsonRequestBehavior.AllowGet);
            }


            // return Json(artSubName.ToArray(), JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// use in page ArticleEdit to get a subject quickly autocomplete, ajax
        /// </summary>
        /// <param name="term"></param>
        /// <returns></returns>
        //public ActionResult GetSbject2(string term)
        //{
        //    term = term.Trim();
        //    //   term = term.Replace(" ", "");
        //    if (term.Length > 1)
        //    {
        //        int op = term.LastIndexOf(",");
        //        term = term.Substring(op + 1);
        //    }
        //    term = term.Trim();
        //    var artSubName = DataSingleton.Instance.GetLinQContext().ArticleSubjects
        //                     .Where(u => u.ArtSubjectName.IndexOf(term) != -1).Take(10)
        //                     .Select(u => new { label = u.ArtSubjectName, id = u.ArtSubjectID });
        //    if (artSubName.Count() == 0)
        //    {
        //        IEnumerable<ArticleSubject> subs = new ArticleSubject[] 
        //                        { 
        //                          new ArticleSubject { ArtSubjectID = 0, ArtSubjectName = "Chủ đề này chưa có" } 
        //                        };
        //        var aa = subs.Select(u => new { label = u.ArtSubjectName, id = u.ArtSubjectID });
        //        return Json(aa.ToArray(), JsonRequestBehavior.AllowGet);
        //    }
        //    else
        //    {
        //        return Json(artSubName.ToArray(), JsonRequestBehavior.AllowGet);
        //    }


        //    // return Json(artSubName.ToArray(), JsonRequestBehavior.AllowGet);
        //}


        /// <summary>
        /// use in page ArticleEdit to create a subject quickly, ajax
        /// </summary>
        /// <param name="articleTypeID"></param>
        /// <param name="artid"></param>
        /// <returns></returns>
        [CheckMemberVisibility, Authorize(Roles = ConstantClass.articleAuthentication)]
        public ActionResult QuickCreateSubject(string newSubjectName)
        {
            newSubjectName = newSubjectName.Trim();
            if (newSubjectName != null && newSubjectName.ToString() != "")
            {
                if (DataSingleton.Instance.GetArticleSubjectModels().InsertSubject(newSubjectName, HttpContext.User.Identity.Name))
                {
                    return Content("Đã tạo chủ đề \"" + newSubjectName + "\"");
                }
                else
                {
                    return Content("Thất bại, vui lòng thử lại.");
                }
            }
            else
            {
                return Content("Vui lòng nhập chủ đề.");
            }
        }

        [CheckMemberVisibility, Authorize(Roles = ConstantClass.articleAuthentication)]
        public ActionResult ArticleSubject_BackEnd(int artSubjectID = 0, bool deleteArtSubFail = false)
        {

            if (artSubjectID == 0)
            {
                ViewBag.ArticleSubjectDetails = new ArticleSubject();
            }
            else
            {
                ViewBag.ArticleSubjectDetails = DataSingleton.Instance.GetArticleSubjectModels().GetArticleSubjectDetails(artSubjectID);
            }

            if (deleteArtSubFail)
            {
                ViewBag.deleteArtSubFail = true;
            }

            return View("/Views/BackEnd/ArticleSubject/ArticleSubjectList.cshtml", DataSingleton.Instance.GetArticleSubjectModels().GetArticleListBackEnd());
        }


        [CheckMemberVisibility, Authorize(Roles = ConstantClass.articleAuthentication), HttpPost, ValidateInput(false)]
        [ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult MakeArticleSubject(ArticleSubject artSub)
        {
            if (artSub.ArtSubjectName != null)
            {
                string memberName = HttpContext.User.Identity.Name;
                //Edit tag
                if (artSub.ArtSubjectID > 0)
                {
                    DataSingleton.Instance.GetArticleSubjectModels().EditArticleSubject(artSub, memberName);
                }
                //Create tag
                else
                {
                    DataSingleton.Instance.GetArticleSubjectModels().InsertSubject(artSub.ArtSubjectName, memberName);
                }
            }

            return RedirectToAction("ArticleSubject_BackEnd", "ArticleSubject", new { articleSubjectID = artSub.ArtSubjectID });
        }

        [CheckMemberVisibility]
        [Authorize(Roles = ConstantClass.articleAuthentication), HttpPost, ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult ArticleSubjectDelete(int artSubjectID)
        {
            ViewBag.ArticleSubjectDetails = new ArticleSubject();

            if (!DataSingleton.Instance.GetArticleSubjectModels().DeleteArticleSubject(artSubjectID))
            {
                ModelState.Clear();
                return RedirectToAction("ArticleSubject_BackEnd", "ArticleSubject", new { deleteArtSubFail = true });
            }
            return RedirectToAction("ArticleSubject_BackEnd", "ArticleSubject", new { deleteArtSubFail = false });
        }


        [CheckMemberVisibility, Authorize(Roles = ConstantClass.articleAuthentication)]
        public ActionResult ViewArticlesOfArticleSubject(int artSubjectID = 0, string artSubjectName = "")
        {
            if (artSubjectID == 0)
            {
                return RedirectToAction("ArticleSubject_BackEnd", "ArticleSubject");
            }

            ViewBag.ArtSubjectName = artSubjectName;

            return View("/Views/BackEnd/ArticleSubject/ArticlesOfArticleSubject.cshtml", 
                DataSingleton.Instance.GetArticleSubjectModels().GetArticlesOfArticleSubject(artSubjectID));
        }

        [CheckMemberVisibility]
        [Authorize(Roles = ConstantClass.articleAuthentication), HttpPost, ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult DeleteArticle(int articleID, int artSubjectID, string artSubjectName)
        {
            DataSingleton.Instance.GetArticleModels().DeleteArticle(articleID, Path.Combine(Request.PhysicalApplicationPath.ToString(),
                            "Content/images/article/thumbnail/"));

            return RedirectToAction("ViewArticlesOfArticleSubject", "ArticleSubject", new { artSubjectID = artSubjectID, artSubjectName = artSubjectName });
        }

        //[HttpGet]
        //public ActionResult ChangeArticleVisible(int articleID, bool articleVisible, int artSubjectID)
        //{
        //    DataSingleton.Instance.GetArticleModels().ChangeArticleVisible(articleID, articleVisible);
        //    return RedirectToAction("ViewArticlesOfArticleSubject", "ArticleSubject", new {artSubjectID = artSubjectID });
        //}


        #endregion



    }
}
